
package com.coolgor.coldot.service.stock.impl;

import com.coolgor.coldot.common.PageCalculator;
import com.coolgor.coldot.entity.stock.MarketMinite;
import com.coolgor.coldot.dao.stock.MarketMiniteDao;
import com.coolgor.coldot.dto.stock.MarketMiniteExecution;
import com.coolgor.coldot.enums.stock.MarketMiniteOperationStateEnum;
import com.coolgor.coldot.exceptions.stock.MarketMiniteOperationException;
import com.coolgor.coldot.service.stock.MarketMiniteService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


@Service
public class MarketMiniteServiceImpl implements MarketMiniteService {

	private static Logger log = LoggerFactory.getLogger(MarketMiniteServiceImpl.class);

	@Autowired
	MarketMiniteDao marketMiniteDao;

	@Override
	public MarketMiniteExecution getByID(Long recordID) {
		// 空值判断
		if (null == recordID || recordID.equals("")) {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.NULL_ID);
		}
		MarketMinite marketMinite = marketMiniteDao.queryByID(recordID);
		if (marketMinite != null) {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.SUCCESS, marketMinite);
		} else {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.NULL);
		}
	}

	@Override
	public MarketMiniteExecution getList(MarketMinite marketMiniteCondition, int pageIndex, int pageSize) {
		// 将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		// 依据查询条件，调用dao层返回相关的查询结果列表
		List<MarketMinite> list = marketMiniteDao.queryList(marketMiniteCondition, rowIndex, pageSize);
		// 依据相同的查询条件，返回查询结果总数
		int count = marketMiniteDao.queryCount(marketMiniteCondition);
		MarketMiniteExecution result = new MarketMiniteExecution();
		if (list != null) {
			result.setState(MarketMiniteOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(MarketMiniteOperationStateEnum.SUCCESS.getStateInfo());
			result.setMarketMiniteList(list);
			result.setCount(count);
		} else {
			result.setState(MarketMiniteOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public MarketMiniteExecution add(MarketMinite marketMinite) throws MarketMiniteOperationException {
		// 空值判断
		if (marketMinite == null) {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行插入数据操作 
			effectedNum = marketMiniteDao.insert(marketMinite);
			if (effectedNum <= 0) {
				throw new MarketMiniteOperationException("MarketMinite信息插入失败");
			}
		} catch (Exception e) {
			throw new MarketMiniteOperationException("Add marketMinite error:" + e.getMessage());
		}
		return new MarketMiniteExecution(MarketMiniteOperationStateEnum.SUCCESS, marketMinite);
	}

	@Override
	@Transactional
	public MarketMiniteExecution modify(MarketMinite marketMinite) throws MarketMiniteOperationException {
		// 空值判断
		if (marketMinite == null) {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行修改更新操作 
			effectedNum = marketMiniteDao.update(marketMinite);
			if (effectedNum <= 0) {
				throw new MarketMiniteOperationException("MarketMinite信息更新失败");
			}
		} catch (Exception e) {
			throw new MarketMiniteOperationException("Update marketMinite error:" + e.getMessage());
		}
		return new MarketMiniteExecution(MarketMiniteOperationStateEnum.SUCCESS, marketMinite);
	}

	@Override
	@Transactional
	public MarketMiniteExecution remove(Long recordID) throws MarketMiniteOperationException{
		// 空值判断
		if (recordID == null) {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = marketMiniteDao.delete(recordID);
			if (effectedNum <= 0) {
				throw new MarketMiniteOperationException("MarketMinite单条信息删除失败");
			}
		} catch (Exception e) {
			throw new MarketMiniteOperationException("Delete marketMinite error:" + e.getMessage());
		}
		return new MarketMiniteExecution(MarketMiniteOperationStateEnum.SUCCESS);
	}

	@Override
	@Transactional
	public MarketMiniteExecution remove(MarketMinite marketMinite) throws MarketMiniteOperationException{
		// 空值判断
		if (marketMinite == null) {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = marketMiniteDao.delete(marketMinite.getRecordID());
			if (effectedNum <= 0) {
				throw new MarketMiniteOperationException("MarketMinite单条信息删除失败");
			}
		} catch (Exception e) {
			throw new MarketMiniteOperationException("Delete marketMinite error:" + e.getMessage());
		}
		return new MarketMiniteExecution(MarketMiniteOperationStateEnum.SUCCESS, marketMinite);
	}

	@Override
	@Transactional
	public MarketMiniteExecution removeBatchByID(List<Long> recordIDList) throws MarketMiniteOperationException{
		// 空值判断
		if (recordIDList == null || recordIDList.size() < 1) {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = marketMiniteDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new MarketMiniteOperationException("MarketMinite多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new MarketMiniteOperationException("Batch Delete marketMinite error:" + e.getMessage());
		}
		MarketMiniteExecution exe = new MarketMiniteExecution(MarketMiniteOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	@Transactional
	public MarketMiniteExecution removeBatch(List<MarketMinite> marketMiniteList) throws MarketMiniteOperationException{
		// 空值判断
		if (marketMiniteList == null) {
			return new MarketMiniteExecution(MarketMiniteOperationStateEnum.NULL);
		} 
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<marketMiniteList.size(); i++) {
			recordIDList.add(marketMiniteList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = marketMiniteDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new MarketMiniteOperationException("MarketMinite多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new MarketMiniteOperationException("Batch Delete marketMinite error:" + e.getMessage());
		}
		MarketMiniteExecution exe = new MarketMiniteExecution(MarketMiniteOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

}
